package com.ckh.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ckh.springboot.entity.StudentClock;
import com.ckh.springboot.entity.SysClass;
import com.ckh.springboot.entity.User;
import com.ckh.springboot.mapper.StudentClockMapper;
import com.ckh.springboot.service.IStudentClockService;
import com.ckh.springboot.service.ISysClassService;
import com.ckh.springboot.service.IUserService;
import com.ckh.springboot.vo.StudentClockListVO;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.StrUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentClockServiceImpl extends ServiceImpl<StudentClockMapper, StudentClock> implements IStudentClockService {
    
    @Resource
    private IUserService userService;
    
    @Resource
    private ISysClassService sysClassService;

    @Override
    public Page<StudentClockListVO> getStudentClockList(Page<StudentClock> page, String name, Integer teacherId, Integer userId) {
        LambdaQueryWrapper<StudentClock> wrapper = new LambdaQueryWrapper<>();
        
        if (teacherId != null) {
            // 教师查询：查询该教师创建的所有班级的打卡记录
            List<SysClass> teacherClasses = sysClassService.list(
                new LambdaQueryWrapper<SysClass>()
                    .eq(SysClass::getUserId, teacherId)
            );
            List<Integer> classIds = teacherClasses.stream()
                .map(SysClass::getId)
                .collect(Collectors.toList());
            if (!classIds.isEmpty()) {
                wrapper.in(StudentClock::getClassId, classIds);
            }
        } else if (userId != null) {
            // 学生查询：只查询自己的打卡记录
            wrapper.eq(StudentClock::getUserId, userId);
        }
        // 管理员查询：不加限制条件，查询所有
    
        // 按名字模糊搜索
        if (StrUtil.isNotBlank(name)) {
            List<User> users = userService.list(
                new LambdaQueryWrapper<User>()
                    .like(User::getName, name)
            );
            List<Integer> userIds = users.stream()
                .map(User::getId)
                .collect(Collectors.toList());
            if (!userIds.isEmpty()) {
                wrapper.in(StudentClock::getUserId, userIds);
            }
        }
    
        // 按打卡时间倒序
        wrapper.orderByDesc(StudentClock::getReleaseTime);
    
        // 执行分页查询
        Page<StudentClock> clockPage = page(page, wrapper);
        
        // 转换为VO对象
        Page<StudentClockListVO> resultPage = new Page<>();
        resultPage.setTotal(clockPage.getTotal());
        resultPage.setCurrent(clockPage.getCurrent());
        resultPage.setSize(clockPage.getSize());
        
        List<StudentClockListVO> records = new ArrayList<>();
        for (StudentClock clock : clockPage.getRecords()) {
            StudentClockListVO vo = new StudentClockListVO();
            vo.setId(clock.getId());
            vo.setClockStatus(clock.getClockStatus());
            vo.setClockTime(clock.getClockTime());
            vo.setReleaseTime(clock.getReleaseTime());
            
            // 获取学生信息
            User student = userService.getById(clock.getUserId());
            if (student != null) {
                vo.setStudentName(student.getName());
                vo.setStudentId(student.getStudentId()); // 添加学号字段
            }
            
            // 获取班级名称
            SysClass sysClass = sysClassService.getById(clock.getClassId());
            if (sysClass != null) {
                vo.setClassName(sysClass.getName());
            }
            
            records.add(vo);
        }
        resultPage.setRecords(records);
        
        return resultPage;
    }
}